package com.windfone.common;

import java.sql.ResultSet;
import java.sql.Types;

public class SQLUtil {

	public static String[] JDBC_TYPE_MAP = { "CHAR", "VARCHAR", "LONGVARCHAR",
			"NUMERIC", "DECIMAL", "BIT", "TINYINT", "SMALLINT", "INTEGER",
			"BIGINT", "REAL", "FLOAT", "DOUBLE", "BINARY", "VARBINARY",
			"LONGVARBINARY", "DATE", "TIME", "TIMESTAMP" };
	public static String[] JAVA_TYPE_MAP = { "String", "String", "String",
			"BigDecimal", "BigDecimal", "boolean", "byte", "short", "int",
			"long", "float", "double", "double", "byte[]", "byte[]", "byte[]",
			"Date", "Time", "Timestamp" };
	public static String[] JAVA_IMPORT_MAP = { "", "", "",
			"java.math.BigDecimal", "java.math.BigDecimal", "", "", "", "", "",
			"", "", "", "", "", "", "java.sql.Date", "java.sql.Time",
			"java.sql.Timestamp" };
	public static String[] JAVA_TYPE_MAP_JDBC_JAVA = { "String", "String",
			"java.math.BigDecimal", "boolean", "byte", "short", "int", "long",
			"float", "double", "byte[]", "byte[]", "Date", "Time", "Timestamp" };
	public static String[] JAVA_TYPE_MAP_JDBC_JDBC = { "VARCHAR",
			"LONGVARCHAR", "NUMERIC", "BIT", "TINYINT", "SMALLINT", "INTEGER",
			"BIGINT", "REAL", "DOUBLE", "VARBINARY", "LONGVARBINARY", "DATE",
			"TIME", "TIMESTAMP" };
	public static String[] JAVA_TYPE_MAP_FULL_PATH = { "java.lang.String",
			"java.lang.String", "java.lang.String", "java.math.BigDecimal",
			"java.math.BigDecimal", "boolean", "byte", "short", "int", "long",
			"float", "double", "double", "byte[]", "byte[]", "byte[]",
			"java.util.Date", "java.sql.Time", "java.sql.Timestamp" };

	public static String changeJDBCTypeToJavaType(String jdbcTypeName) {
		String typeName = jdbcTypeName.toUpperCase();
		String rtv = "";
		for (int i = 0; i < JDBC_TYPE_MAP.length; i++) {
			if (JDBC_TYPE_MAP[i].equals(typeName)) {
				rtv = JAVA_TYPE_MAP[i];
				break;
			}
		}
		return rtv;
	}

	public static String getImportPath(String jdbcTypeName) {
		String typeName = jdbcTypeName.toUpperCase();
		String rtv = "";
		for (int i = 0; i < JDBC_TYPE_MAP.length; i++) {
			if (JDBC_TYPE_MAP[i].equals(typeName)) {
				rtv = JAVA_IMPORT_MAP[i];
				break;
			}
		}
		return rtv;
	}

	public static String getDataType(int type, int scale) {
		String dataType = "";
		switch (type) {
		case Types.LONGVARCHAR: // -1
			dataType = "Long";
			break;
		case Types.CHAR: // 1
			dataType = "Character";
			break;
		case Types.INTEGER: // 1
			dataType = "int";
			break;
		case Types.NUMERIC: // 2
			switch (scale) {
			case 0:
				dataType = "Number";
				break;
			case -127:
				dataType = "Float";
				break;
			default:
				dataType = "Number";
			}
			break;
		case Types.VARCHAR: // 12
			dataType = "String";
			break;
		case Types.DATE: // 91
			dataType = "Date";
			break;
		case Types.TIMESTAMP: // 93
			dataType = "Date";
			break;
		case Types.BLOB:
			dataType = "Blob";
			break;
		default:
			dataType = "String";
		}
		return dataType;
	}

	public static Object getValue(ResultSet rs, String fieldName, Class<?> type) {
		try {
			if (type.getName().equalsIgnoreCase("java.lang.String")) {
				return rs.getString(fieldName);
			} else if (type.getName().equalsIgnoreCase("boolean")) {
				return rs.getBoolean(fieldName);
			} else if (type.getName().equalsIgnoreCase("int")) {
				return rs.getInt(fieldName);
			} else if (type.getName().equalsIgnoreCase("long")) {
				return rs.getLong(fieldName);
			} else if (type.getName().equalsIgnoreCase("float")) {
				return rs.getFloat(fieldName);
			} else if (type.getName().equalsIgnoreCase("double")) {
				return rs.getDouble(fieldName);
			} else if (type.getName().equalsIgnoreCase("byte")) {
				return rs.getByte(fieldName);
			} else if (type.getName().equalsIgnoreCase("java.util.Date") || type.getName().equalsIgnoreCase("java.sql.Time")) {
				return rs.getDate( fieldName);
			} else if (type.getName().equalsIgnoreCase("java.util.Date") || type.getName().equalsIgnoreCase("java.sql.Timestamp")) {
				return rs.getTimestamp( fieldName);
			}				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
